package com.ca.project.Server.Modules.User.Controller;

import com.ca.project.Server.Modules.User.Context.UserRegisterContext;
import com.ca.project.Server.Modules.User.Converter.UserConverter;
import com.ca.project.Server.Modules.User.Entity.UserId;
import com.ca.project.Server.Modules.User.PO.TokenValidPO;
import com.ca.project.Server.Modules.User.PO.UserRegisterPo;
import com.ca.project.Server.Modules.User.Service.UserIdService;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.User.Controller
 * @NAME: UserController
 * @USER: ASUS
 * @DATE: 2023/9/4
 * @TIME: 下午 04:51
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "小程序用户模块")
public class UserController {

    @Autowired
    private UserIdService userIdService;

    @Resource
    private UserConverter userConverter;

    @ApiOperation(
            value = "微信小程序用户登录接口",
            notes = "该接口提供了用户注册登录的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/register")
    public R Register(@Validated @RequestBody UserRegisterPo userRegisterPo) {
        UserRegisterContext userRegisterContext = userConverter.userRegisterPO2Register(userRegisterPo);
        String token = userIdService.login(userRegisterContext);
        return R.data(token);
    }

    @ApiOperation(
            value = "查询token是否有效接口",
            notes = "该接口提供了查询token是否有效的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/token")
    public R TokenValid(@Validated @RequestBody TokenValidPO tokenValidPO){
        String token = tokenValidPO.getToken();
        boolean success = userIdService.valid(token);
        return R.data(success);
    }

}
